package fr.upmc.contact;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import fr.upmc.contact.model.Contact;

public class ContactAdapter extends ArrayAdapter<Contact> {
	
	/**
	 * List of Contact
	 */
	private List<Contact> contact;
	/**
	 * Only use for TEACHING :
	 * Total number of allocation when using 2 different cells views
	 */
	private Map<Integer,Integer> counters;
	
	/**
	 * Constructor with initialization of the Contact Collection
	 * @param context The initial context
	 * @param textViewResourceId Identification of the ressource ID
	 * @param contact The collection of contact
	 */
	public ContactAdapter(Context context, int textViewResourceId,
			List<Contact> contact) {
		super(context, textViewResourceId, contact);
		this.contact = contact;
		counters = new HashMap<Integer,Integer>();
		counters.put(0, 0);
		counters.put(1, 0);
	}
	
	/**
	 * 
	 * @return The model
	 */
	public List<Contact> getModel(){
		return contact;
	}
	
	/**
	 * Adding a collection of contacts
	 * @param list Collection of contacts
	 */
	public void addAll(List<Contact> list){
		contact.addAll(list);
		notifyDataSetChanged();
	}
	
	
	/**
	 * How to manage different Cell View in the list ?
	 * This function must be override if you want to manage different
	 * cells views in the list.
	 * Here you must specify switch the item position (by acceding to the object)
	 * which type of view to use.
	 * 
	 * INTERNAL USED :
	 * The base class Adapter uses this operation before call get view for know
	 * which kind of view we can convert and reuse instead of re-allocate an
	 * new object.
	 * 
	 *  1) Get the type of view (for obejct at position)
	 *  2) Check if there is a view can be converted
	 *  3)   - convertView = null OR convertView = the view find with good ID
	 *  4) Call getView (...) 
	 * 
	 */
	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		//return super.getItemViewType(position);
		Contact c = contact.get(position);
		if (c.getPhoto() == null)
			return 0;
		return 1;
	}

	/**
	 * How to manage different Cell View in the list ?
	 * Simply, here you must define the total number of different
	 * cell view.
	 * By default, when all cells views are the same, it returns 1
	 * 
	 * INTERNAL USED :
	 * Maybe it is used by the internal Adapter Class for know how
	 * many different cells views there are
	 */
	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 2;//super.getViewTypeCount();
	}

	/**
	 * VERY IMPORTANT NOTE :
	 *   For avoid to much allocation of Cell view, android works like iphone
	 *   list.
	 *   	1) First we call getView only for the cell which are visible by user
	 *   	2) System will allocate only necessary object, when one View cell is
	 *   not in the visible screen, Android will re-used the view. SO IT IS VERY
	 *   IMPORTANT TO INITIALIZE ALL FIELD IF VIEW ALREADY EXIST
	 *   	3) In case of manage different custom cell views, we must check
	 *   identification ID (link with the XML ID) of the view before use it.
	 *   It avoids to find elements which can be not exist ...
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			// Create a new layout for the current cell
			// Cell must be same have declared on XML Layout
			LayoutInflater li = (LayoutInflater) this.getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			/**
			 * If you manage to many views, you must ensure to allocate
			 * the right view switch the Item type
			 */
			switch (getItemViewType(position)){
				case 0:
					view = li.inflate(R.layout.cell_list, null);
					counters.put(0, counters.get(0)+1);
					break;
				case 1:
					view = li.inflate(R.layout.cell_list_2, null);
					counters.put(1, counters.get(1)+1);
					break;
			}
		}
		
		/**
		 * TAKECARE :
		 * If you work with different views, you must check the item type,
		 * for ensure you will work with good ID variable from XML GUI when
		 * using operation like (findViewById, ...) 
		 */
		Contact current = getItem(position);
		if (current != null) {
			TextView txt = (TextView) view.findViewById(R.id.TextView01);
			txt.setText(current.getFirstname());
			ImageView iv = (ImageView) view.findViewById(R.id.ImageView01);
			Log.d("ContactAdapter" , current.getFirstname() +" "+current.getPhoto());
			if (current.getPhoto() != null)
				iv.setImageURI(current.getPhoto());
			else
				iv.setImageResource(R.drawable.lgandroid);
		}
		
		Log.d("ContactAdapter" , "View 1 : "+counters.get(0) +" - View 2" + counters.get(1));
		return view;
	}

}
